﻿using System;
using System.Collections.Generic;
using System.Text;

namespace YouHP.BLL.MedicalGraph
{
    public class GraphDataItemInfo : IComparable
    {
        public DateTime DateTime1 { get; set; }
        public DateTime DateTime2 { get; set; }
        public string Value { get; set; }
        public string Value2 { get; set; }

        #region IComparable 成员

        int IComparable.CompareTo(object obj)
        {
            GraphDataItemInfo a = this;
            GraphDataItemInfo b = obj as GraphDataItemInfo;
            if (a.DateTime1 > b.DateTime1) return 1;
            else if (a.DateTime1 == b.DateTime1) return 0;
            else return -1;
        }

        #endregion
    }

    public class GraphDataVariableInfo
    {
        public List<GraphDataItemInfo> DataItemList { get; set; }

        public GraphDataVariableInfo()
        {
            DataItemList = new List<GraphDataItemInfo>();
        }

        public void AddData(GraphDataItemInfo info)
        {
            int idx = DataItemList.BinarySearch(info);
            if (idx < 0) DataItemList.Insert(~idx, info);
        }
    }

    public class GraphDataManager
    {
        public Dictionary<string, GraphDataVariableInfo> DataVariableList { get; set; }
        public DateTime MaxDateTime { get; set; }
        public DateTime MinDateTime { get; set; }

        public GraphDataManager()
        {
            DataVariableList = new Dictionary<string, GraphDataVariableInfo>();
            MaxDateTime = DateTime.Now;
            MinDateTime = DateTime.Now;
        }

        public GraphDataVariableInfo GetDataVariable(string key)
        {
            if (DataVariableList == null) return null;
            if (DataVariableList.ContainsKey(key) == false) return null;
            return DataVariableList[key];
        }

        public void AddData(string key,DateTime dt1, DateTime dt2, string value)
        {
            if (DataVariableList.ContainsKey(key) == false)
            {
                DataVariableList.Add(key, new GraphDataVariableInfo());
            }
            GraphDataVariableInfo varInfo = DataVariableList[key];
            GraphDataItemInfo dataInfo = new GraphDataItemInfo();
            dataInfo.DateTime1 = dt1;
            dataInfo.DateTime2 = dt2;
            dataInfo.Value = value;
            varInfo.AddData(dataInfo);
            if (MinDateTime > dt1) MinDateTime = dt1;
            if (MaxDateTime < dt1) MaxDateTime = dt1;
        }

        // 为治疗准备的
        public void AddData(string key, DateTime dt1, DateTime dt2, string value,string value2)
        {
            if (DataVariableList.ContainsKey(key) == false)
            {
                DataVariableList.Add(key, new GraphDataVariableInfo());
            }
            GraphDataVariableInfo varInfo = DataVariableList[key];
            GraphDataItemInfo dataInfo = new GraphDataItemInfo();
            dataInfo.DateTime1 = dt1;
            dataInfo.DateTime2 = dt2;
            dataInfo.Value = value;
            dataInfo.Value2 = value2;
            varInfo.AddData(dataInfo);
            if (MinDateTime > dt1) MinDateTime = dt1;
            if (MaxDateTime < dt2) MaxDateTime = dt2;
        }

        public GraphDataVariableInfo GetVariable(string key)
        {
            if (DataVariableList.ContainsKey(key)) return null;
            return DataVariableList[key];
        }
    }
}
